package es.ikex.servlet;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Busqueda extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Enumeration<String> parametros =request.getParameterNames();
		
		String where = "";
		String against = "";
		while(parametros.hasMoreElements()){
			String nombre = parametros.nextElement();
			String parametro = request.getParameter(nombre);
			if(parametro != null && !parametro.equals("") && !nombre.equals("buscar")){
				if(nombre.equals("concepto") || nombre.equals("descripcion")){
					against = parametro + " " + against;
					//where = where + "select * from productos where match(concepto, descripcion) against('mueble')"
				}else if(nombre.indexOf("precio") == -1){
					where = where + " " + nombre + " like '%" + parametro + "%' and";
				}
				else{
					if(nombre.indexOf("Max") == -1){
						where = where + " precio>" + parametro + " and";
					}
					else{
						where = where + " precio<" + parametro + " and";
					}
				}
			}
			
		}
		if(against.length()>1){
			where = " where " + " match (concepto, descripcion) against ('" + against + "') " + where;
		}
		else{
			where = " where " + where;
		}
		if(where.length() < 4){
			where = " where 'a'='b'";
		}
		
		request.setAttribute("where", where);
		request.getRequestDispatcher("jsp/resultadoBusqueda.jsp").forward(request, response);
	}

}
